package com.ivo.biz;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ivo.util.PMSDBUtil;

@Controller
@RequestMapping("/restful")
public class PMSRestfulController{
	
	@Resource(name = "dataSource")
	private DataSource dataSource;
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/getCELPerformance.do")
	@ResponseBody
	public void getCELPerformance(HttpServletRequest request, HttpServletResponse response) throws IOException {
		StringBuffer jsonSb = new StringBuffer();
		List<Object> params = new ArrayList<>();
		System.out.println("X");
		PMSDBUtil db = new PMSDBUtil();
		List<Object> alllist = db.excuteQuery("SELECT `MONTH`,EMP_ID,EMP_NAME,SCORE FROM dl_m_performance WHERE FACTORY = 'CEL' AND  `MONTH` = (SELECT MAX(MONTH) FROM dl_m_performance)",params.toArray());
		Object[] arr = alllist.toArray();
		if(arr.length > 0 ){
			for(int i = 0; i < arr.length; i++){
				HashMap<String, String> map = (HashMap<String, String>) arr[i];
				if(jsonSb.length() > 0)
					jsonSb.append(",");
				jsonSb.append("{");
				jsonSb.append("\"month\":\"" + map.get("MONTH") + "\",");
				jsonSb.append("\"emp_id\":\"" + map.get("EMP_ID") + "\",");
				jsonSb.append("\"emp_name\":\"" + map.get("EMP_NAME") + "\",");
				jsonSb.append("\"score\":\"" + String.valueOf(map.get("SCORE")) + "\"");
				jsonSb.append("}");
			}
		}
        
        String result = "["+jsonSb.toString()+"]";
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(result); 
	}
}